package com.google.android.exoplayer2.source.ads;

import android.net.Uri;
import android.os.Handler;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.upstream.DataSpec;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import o.abr;
import o.abr.c;
import o.ahv;
import o.aib;
import o.aib.b;
import o.aih;
import o.aim;
import o.aiz;
import o.aiz.e;
import o.ajb;
import o.ajc;
import o.ajd;
import o.aje;
import o.alc;
import o.alx;
import o.amm;

public final class AdsMediaSource extends ahv<o.aim.a> {
    private static final o.aim.a ˊ = new o.aim.a(new Object());
    private abr ʻ;
    private final c ʼ;
    private d ʽ;
    private AdPlaybackState ˊॱ;
    private final aiz ˋ;
    private final e ˎ;
    private final b ˏ;
    private aim[][] ˏॱ;
    private Object ͺ;
    private final aim ॱ;
    private abr[][] ॱˊ;
    private final Handler ॱॱ;
    private final Map<aim, List<aib>> ᐝ;

    public static final class AdLoadException extends IOException {
        public final int ˋ;

        @Documented
        @Retention(RetentionPolicy.SOURCE)
        public @interface Type {
        }

        public static AdLoadException ˊ(Exception exception) {
            return new AdLoadException(0, exception);
        }

        private AdLoadException(int i, Exception exception) {
            super(exception);
            this.ˋ = i;
        }
    }

    public final class a implements b {
        private final int ˊ;
        private final int ˋ;
        private final Uri ˎ;
        final /* synthetic */ AdsMediaSource ˏ;

        public a(AdsMediaSource adsMediaSource, Uri uri, int i, int i2) {
            this.ˏ = adsMediaSource;
            this.ˎ = uri;
            this.ˊ = i;
            this.ˋ = i2;
        }

        public void ˋ(o.aim.a aVar, IOException iOException) {
            this.ˏ.ˏ(aVar).ˎ(new DataSpec(this.ˎ), this.ˎ, Collections.emptyMap(), 6, -1, 0, 0, AdLoadException.ˊ(iOException), true);
            this.ˏ.ॱॱ.post(new ajb(this, iOException));
        }

        private /* synthetic */ void ˊ(IOException iOException) {
            this.ˏ.ˋ.ˋ(this.ˊ, this.ˋ, iOException);
        }
    }

    public final class d implements aiz.b {
        final /* synthetic */ AdsMediaSource ˊ;
        private final Handler ˋ = new Handler();
        private volatile boolean ˎ;

        public d(AdsMediaSource adsMediaSource) {
            this.ˊ = adsMediaSource;
        }

        public void ˊ() {
            this.ˎ = true;
            this.ˋ.removeCallbacksAndMessages(null);
        }
    }

    public /* synthetic */ void ˋ(Object obj, aim o_aim, abr o_abr, @Nullable Object obj2) {
        ॱ((o.aim.a) obj, o_aim, o_abr, obj2);
    }

    public void ॱ(@Nullable alx o_alx) {
        super.ॱ(o_alx);
        d dVar = new d(this);
        this.ʽ = dVar;
        ˊ((Object) ˊ, this.ॱ);
        this.ॱॱ.post(new ajc(this, dVar));
    }

    private /* synthetic */ void ॱ(d dVar) {
        this.ˋ.ˎ(dVar, this.ˎ);
    }

    public aih ˊ(o.aim.a aVar, alc o_alc, long j) {
        if (this.ˊॱ.ˎ <= 0 || !aVar.ˊ()) {
            aih o_aib = new aib(this.ॱ, aVar, o_alc, j);
            o_aib.ˏ(aVar);
            return o_aib;
        }
        aim ˊ;
        int i = aVar.ॱ;
        int i2 = aVar.ˊ;
        Uri uri = this.ˊॱ.ˋ[i].ˏ[i2];
        if (this.ˏॱ[i].length <= i2) {
            ˊ = this.ˏ.ˊ(uri);
            if (i2 >= this.ˏॱ[i].length) {
                int i3 = i2 + 1;
                this.ˏॱ[i] = (aim[]) Arrays.copyOf(this.ˏॱ[i], i3);
                this.ॱˊ[i] = (abr[]) Arrays.copyOf(this.ॱˊ[i], i3);
            }
            this.ˏॱ[i][i2] = ˊ;
            this.ᐝ.put(ˊ, new ArrayList());
            ˊ((Object) aVar, ˊ);
        }
        ˊ = this.ˏॱ[i][i2];
        o_aib = new aib(ˊ, aVar, o_alc, j);
        o_aib.ˏ(new a(this, uri, i, i2));
        List list = (List) this.ᐝ.get(ˊ);
        if (list == null) {
            o_aib.ˏ(new o.aim.a(this.ॱˊ[i][i2].ˋ(0), aVar.ˎ));
            return o_aib;
        }
        list.add(o_aib);
        return o_aib;
    }

    public void ॱ(aih o_aih) {
        aib o_aib = (aib) o_aih;
        List list = (List) this.ᐝ.get(o_aib.ˋ);
        if (list != null) {
            list.remove(o_aib);
        }
        o_aib.ʻ();
    }

    public void ˎ() {
        super.ˎ();
        this.ʽ.ˊ();
        this.ʽ = null;
        this.ᐝ.clear();
        this.ʻ = null;
        this.ͺ = null;
        this.ˊॱ = null;
        this.ˏॱ = new aim[0][];
        this.ॱˊ = new abr[0][];
        Handler handler = this.ॱॱ;
        aiz o_aiz = this.ˋ;
        o_aiz.getClass();
        handler.post(new ajd(o_aiz));
    }

    protected void ॱ(o.aim.a aVar, aim o_aim, abr o_abr, @Nullable Object obj) {
        if (aVar.ˊ()) {
            ˏ(o_aim, aVar.ॱ, aVar.ˊ, o_abr);
        } else {
            ˎ(o_abr, obj);
        }
    }

    @Nullable
    protected o.aim.a ˊ(o.aim.a aVar, o.aim.a aVar2) {
        return aVar.ˊ() ? aVar : aVar2;
    }

    private void ˎ(abr o_abr, Object obj) {
        boolean z = true;
        if (o_abr.ˎ() != 1) {
            z = false;
        }
        amm.ˊ(z);
        this.ʻ = o_abr;
        this.ͺ = obj;
        ˋ();
    }

    private void ˏ(aim o_aim, int i, int i2, abr o_abr) {
        boolean z = true;
        if (o_abr.ˎ() != 1) {
            z = false;
        }
        amm.ˊ(z);
        this.ॱˊ[i][i2] = o_abr;
        List list = (List) this.ᐝ.remove(o_aim);
        if (list != null) {
            Object ˋ = o_abr.ˋ(0);
            for (int i3 = 0; i3 < list.size(); i3++) {
                aib o_aib = (aib) list.get(i3);
                o_aib.ˏ(new o.aim.a(ˋ, o_aib.ˏ.ˎ));
            }
        }
        ˋ();
    }

    private void ˋ() {
        if (this.ˊॱ != null && this.ʻ != null) {
            this.ˊॱ = this.ˊॱ.ˊ(ˎ(this.ॱˊ, this.ʼ));
            ˏ(this.ˊॱ.ˎ == 0 ? this.ʻ : new aje(this.ʻ, this.ˊॱ), this.ͺ);
        }
    }

    private static long[][] ˎ(abr[][] o_abrArr, c cVar) {
        long[][] jArr = new long[o_abrArr.length][];
        for (int i = 0; i < o_abrArr.length; i++) {
            jArr[i] = new long[o_abrArr[i].length];
            for (int i2 = 0; i2 < o_abrArr[i].length; i2++) {
                long j;
                long[] jArr2 = jArr[i];
                if (o_abrArr[i][i2] == null) {
                    j = -9223372036854775807L;
                } else {
                    j = o_abrArr[i][i2].ˏ(0, cVar).ˏ();
                }
                jArr2[i2] = j;
            }
        }
        return jArr;
    }
}
